﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Autofac;
using Autofac.Core;
using OrchardFork.Framework.Environment;
using NPoco;

namespace OrchardFork.Framework.Data
{
    public class Record<T> where T : new()
    {
        private static IDatabase Database
        {
            get
            {
                return DefaultHostContainer.Container.Resolve<IBaseDao>().GetDatabase();
            }
        }
        public void Save()
        {
            Database.Save<T>(this);
        }

        public int Update()
        {
            return Database.Update(this);
        }

        public int Update(string sql, params object[] args)
        {
            return Database.Update<T>(sql, args);
        }

        public int Update(object poco, object primaryKeyValue, IEnumerable<string> columns)
        {
            return Database.Update(poco, primaryKeyValue, columns);
        }

        public static T Single(String sql)
        {
            return Database.Single<T>(sql);
        }


        public static T SingleOrDefault(string sql, params object[] args)
        {
            return Database.SingleOrDefault<T>(sql, args);
        }

        public static T SingleById(object primarykey)
        {
            return Database.SingleById<T>(primarykey);
        }

        public static T SingleOrDefaultById(object primarykey)
        {
            return Database.SingleOrDefaultById<T>(primarykey);
        }

        public static void Delete(Sql sql)
        {
            Database.Delete(sql);
        }

        public static void Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
        {
            Database.Delete(tableName, primaryKeyName, poco, primaryKeyValue);
        }

        public static void Delete(T poco)
        {
            Database.Delete(poco);
        }
    }
}
